package cn.tedu;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo17 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scan.nextLine();
        System.out.println("请输入密码");
        String password = scan.nextLine();

        //获取连接
        try (Connection conn = DBUtils.getConn()){
            String sql = "select password from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);//预先编译SQL语句
            ps.setString(1,username);//把输入的username值传入SQL语句
            ResultSet rs = ps.executeQuery();//用结果集接收执行完SQL语句
            if(rs.next()){//如果找到了该用户就比较密码是否一致
                String pw = rs.getString(1);//先接收到查询数据库中对应的密码
                if(pw.equals(password)){//比较用户输入的密码和查询到的
                    System.out.println("登录成功!");//如果一致就成功
                }else System.out.println("密码错误!");//否则就密码错误
            }else System.out.println("用户名不存在!");//都没找到就说明用户名根本不存在
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
